package com.heima.article.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.article.mapper.ArticleConfigMapper;
import com.heima.article.mapper.ArticleContentMapper;
import com.heima.article.mapper.ArticleMapper;
import com.heima.article.service.ArticleService;
import com.heima.model.article.dtos.ArticleDto;
import com.heima.model.article.dtos.ArticleHomeDto;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.article.pojos.ApArticleConfig;
import com.heima.model.article.pojos.ApArticleContent;
import com.heima.model.common.dtos.ResponseResult;
import org.apache.ibatis.annotations.Param;
import org.bouncycastle.asn1.dvcs.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.LinkedList;
import java.util.List;


@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, ApArticle> implements ArticleService {

    @Autowired
    private ArticleMapper articleMapper;

    @Autowired
    private ArticleContentMapper articleContentMapper;

    @Autowired
    private ArticleConfigMapper articleConfigMapper;


    /**
     * 加载首页-加载更多-加载更新 三位一体
     *
     * @param dto  type=1 认为是加载更多，type=2表示加载更新
     * @param type
     * @return
     */

    int pageNum=0;
    int pageNum1=0;
    @Override
    public ResponseResult load(ArticleHomeDto dto, Short type) {
        //TODO
       LambdaQueryWrapper<ApArticle> wrapper = Wrappers.<ApArticle>lambdaQuery();
//        // 转换为 LocalDateTime
//        LocalDateTime maxLocalDateTime = dto.getMaxBehotTime().toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
//        LocalDateTime minLocalDateTime = dto.getMinBehotTime().toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
//        // 格式化输出
//        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        // Page<ApArticle> page = new Page<>(1, dto.getSize());
        //1.查询推荐
//        if (dto.getTag().equals("__all__")) {
//            Page<ApArticle> page = new Page<>(1, dto.getSize());
//            if (type == 1) {
//                page = new Page<>(1, 12);
//                //wrapper.between(ApArticle::getPublishTime,maxLocalDateTime.format(formatter),minLocalDateTime.format(formatter));
//                wrapper.ge(ApArticle::getPublishTime, minLocalDateTime.format(formatter));
//                articleMapper.selectPage(page, wrapper);
//                return ResponseResult.okResult(page.getRecords());
//            }
//            if (type == 2) {
//                page = new Page<>(1, 12);
//                //wrapper.between(ApArticle::getPublishTime,maxLocalDateTime.format(formatter),minLocalDateTime.format(formatter));
//                wrapper.ge(ApArticle::getPublishTime, maxLocalDateTime.format(formatter));
//                articleMapper.selectPage(page, wrapper);
//                return ResponseResult.okResult(page.getRecords());
//            }
//            articleMapper.selectPage(page, null);
//            return ResponseResult.okResult(page.getRecords());
//        }
//        //查询其他
//        Page<ApArticle> page = new Page<>(1, dto.getSize());
//        if (type == 1) {
//            page = new Page<>(1, 12);
//            wrapper.eq(ApArticle::getChannelId, dto.getTag());
//            // wrapper.between(ApArticle::getPublishTime,maxLocalDateTime.format(formatter),minLocalDateTime.format(formatter));
//            wrapper.ge(ApArticle::getPublishTime, minLocalDateTime.format(formatter));
//            articleMapper.selectPage(page, wrapper);
//            return ResponseResult.okResult(page.getRecords());
//        } else if (type == 2) {
//            page = new Page<>(1, 12);
//            wrapper.eq(ApArticle::getChannelId, dto.getTag());
//            //wrapper.between(ApArticle::getPublishTime,maxLocalDateTime.format(formatter),minLocalDateTime.format(formatter));
//            wrapper.ge(ApArticle::getPublishTime, maxLocalDateTime.format(formatter));
//            articleMapper.selectPage(page, wrapper);
//            return ResponseResult.okResult(page.getRecords());
//        }
//        return null;

        Page<ApArticle> page = new Page<>(1, dto.getSize()); // 默认第一页
        //如果type=3则加载首页
        if (type==3){
            if (dto.getTag().equals("__all__")){
                //查询推荐频道的前10条数据
                articleMapper.selectPage(page,null);
                return ResponseResult.okResult(page.getRecords());
            }else {
                //查询其他频道的前10条数据
                wrapper.eq(ApArticle::getChannelId, dto.getTag());
                articleMapper.selectPage(page,wrapper);
                return ResponseResult.okResult(page.getRecords());
            }
        }else if (type==2){
            for (int i = 0; i < 1; i++) {
                pageNum++;
            }
            page = new Page<>(pageNum, dto.getSize());
            wrapper.eq(ApArticle::getChannelId,dto.getTag())
                    .ge(ApArticle::getPublishTime,dto.getMinBehotTime())
                    .orderByDesc(ApArticle::getPublishTime);
            //type=2表示加载更新
            articleMapper.selectPage(page,wrapper);
            return ResponseResult.okResult(page.getRecords());
        }else if (type==1){
            //type=1 认为是加载更多
            for (int i = 0; i < 1; i++) {
                pageNum1++;
            }
            page = new Page<>(pageNum1, dto.getSize());
            wrapper.eq(ApArticle::getChannelId,dto.getTag())
                    .ge(ApArticle::getPublishTime,dto.getMaxBehotTime())
                    .orderByDesc(ApArticle::getPublishTime);
            //type=2表示加载更新
            articleMapper.selectPage(page,wrapper);
            return ResponseResult.okResult(page.getRecords());
        }
        return null;
    }


    /**
     * 保存三剑客
     *
     * @param dto
     * @return
     */
    @Override
    @Transactional
    public ResponseResult save(ArticleDto dto) {
        //TODO
        //1.是否存在文章id
        if (dto.getId()==null){
            //2.不存在保存文章
            ApArticle apArticle = BeanUtil.copyProperties(dto, ApArticle.class);
            articleMapper.insert(apArticle);//插入文章
            ApArticleConfig apArticleConfig = new ApArticleConfig();
            apArticleConfig.setArticleId(apArticle.getId());
            articleConfigMapper.insert(apArticleConfig);
            ApArticleContent apArticleContent = new ApArticleContent();
            apArticleContent.setArticleId(apArticle.getId());
            apArticleContent.setContent(dto.getContent());
            articleContentMapper.insert(apArticleContent);
            return ResponseResult.okResult(apArticle.getId());
        }else {
            //2.2存在修改文章
            ApArticle apArticle = BeanUtil.copyProperties(dto, ApArticle.class);
            LambdaUpdateWrapper<ApArticle> updateWrapper = Wrappers.lambdaUpdate();
            updateWrapper.eq(ApArticle::getId,dto.getId());
            articleMapper.update(apArticle,updateWrapper);
            return ResponseResult.okResult(dto.getId());
        }
    }

}
